package com.zixing.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class TankFrame extends Frame {

    static final int GAME_WIDTH = PropertyMgr.getInt("game-width");
    static final int GAME_HEIGHT = PropertyMgr.getInt("game-height");
    Tank myTank = new Tank(200, 400, Dir.UP, Group.GOOD, this);
    Bullet b = new Bullet(300, 300, Dir.DOWN, Group.GOOD, this);
    List<Bullet> bullets = new ArrayList<>();

    List<Tank> tanks = new ArrayList<>();

    List<Explode> explodes = new ArrayList<>();

    public TankFrame() {
        // 窗口可见
        setVisible(true);
        // 窗口大小
        setSize(GAME_WIDTH, GAME_HEIGHT);
        // 将窗口可见
        setResizable(false);
        setTitle("tank war");
        addKeyListener(new MyKeyListener());
        // window 监听器
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 点击 x 则关闭窗口
                System.exit(0);
            }
        });
    }

    // 双缓存解决闪烁问题
    // 及将内存的内容复制到显存中
    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    // 在窗口中操作
    @Override
    public void paint(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString("子弹数量: " + bullets.size(), 10, 60);
        g.drawString("敌方数量: " + tanks.size(), 10, 80);
        g.drawString("爆炸数量: " + explodes.size(), 10, 100);
        g.setColor(c);
        myTank.paint(g);
        // 删除的时候越界
        // 迭代器不能在其他地方删除，其他地方删除报错
//        for (Bullet b : bullets) {
//            b.paint(g);
//        }
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(g);
        }

        for (int i = 0; i < tanks.size(); i++) {
            tanks.get(i).paint(g);
        }
        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(g);
        }
        for (int i = 0; i < bullets.size(); i++) {
            for (int j = 0; j < tanks.size(); j++) {
                bullets.get(i).collideWith(tanks.get(j));
            }
        }
    }

    class MyKeyListener extends KeyAdapter {

        boolean bL = false;
        boolean bR = false;
        boolean bU = false;
        boolean bD = false;

        // 键盘按下使用
        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                case KeyEvent.VK_CONTROL:
                    myTank.fire();
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }

        // 键盘谈起使用
        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }

        private void setMainTankDir() {
            myTank.setMoving(true);
            if (bL) myTank.setDir(Dir.LEFT);
            if (bU) myTank.setDir(Dir.UP);
            if (bR) myTank.setDir(Dir.RIGHT);
            if (bD) myTank.setDir(Dir.DOWN);
            if (!bD && !bU && !bL && !bR) {
                myTank.setMoving(false);
            }
        }
    }
}
